DEFINE VAR currmth AS CHARACTER FORMAT "x(7)".
DEFINE STREAM bf.
DEFINE VARIABLE csvName AS CHARACTER FORMAT "x(32)" NO-UNDO.
DEFINE VARIABLE outpath AS CHARACTER FORMAT "x(50)" NO-UNDO.

currmth = string(year(TODAY),"9999") + "M" + STRING(MONTH(TODAY),"99"). 

DEFINE TEMP-TABLE r_routing
        FIELD r_part LIKE ro_routing
        FIELD r_op LIKE ro_op
        FIELD r_mth LIKE currmth
        FIELD r_run LIKE ro_run
        INDEX r_part r_part.

EMPTY TEMP-TABLE r_routing.

FOR EACH ro_det WHERE (ro_start = ? OR ro_start <= TODAY)
AND (ro_end = ? OR ro_end >= TODAY):
    CREATE r_routing.
    ASSIGN r_part = ro_routing
           r_op = ro_op
           r_mth  = currmth
           r_run = ro_run.           
END.


FOR EACH ro_det WHERE (ro_start = ? OR ro_start < DATE(MONTH(TODAY),01,YEAR(TODAY)))
AND (ro_end = ? OR ( ro_end <= TODAY AND ro_end >= date(month(TODAY - 30),01,YEAR(TODAY - 30))) OR ro_end >= TODAY )
BREAK BY ro_routing BY ro_op BY ro_end DESCENDING :
    IF NOT CAN-FIND(FIRST r_routing WHERE r_part = ro_routing AND r_mth = string(year(TODAY - 30),"9999") + "M" + STRING(MONTH(TODAY - 30),"99")
                    AND r_op = ro_op) THEN DO:
    CREATE r_routing.
    ASSIGN r_part = ro_routing
           r_op = ro_op
           r_mth  = string(year(TODAY - 30),"9999") + "M" + STRING(MONTH(TODAY - 30),"99")
           r_run = ro_run.   
  END.
END.

outpath =  "\\192.168.12.12\shared$\output\".

/* outpath = "c:\qadguicli\".  */

FOR first code_mstr NO-LOCK WHERE  code_fldname="HKroop" and code_value = "outpath":
END.
if avail code_mstr then do:
    assign outpath = code_cmmt.
END.
csvname = "R7007Dynamic.csv".
csvname = outpath + csvname.


/* csvname = "R7007Dynamic.csv". */
OUTPUT STREAM bf TO VALUE(csvname).
EXPORT STREAM bf DELIMITER "," "d_routing" "d_op" "d_month" "d_run".
FOR EACH r_routing:
    EXPORT STREAM bf DELIMITER "," r_part r_op r_mth r_run.
END.

output stream bf close.

QUIT.
/* CLOSE STREAM . */



